package com.itcast.flink.sources.socket;

import com.itcast.flink.sources.socket.pojo.OrderAddress;
import com.itcast.flink.sources.socket.repository.OrderAddressRepository;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.util.*;

/**
 * <p>Description: </p>
 *
 * @author
 * @version 1.0
 * <p>Copyright:Copyright(c)2020</p>
 * @date
 */
@SpringBootApplication
@ComponentScan(basePackages = {"com.itcast"})
@EnableTransactionManagement
public class OrderAddressSourceApplication {
    
    /**
     * 省市信息
     */
    private static Map<String, List<String>> provinceMap = new HashMap<String, List<String>>();
    
    /**
     * netty服务端的启动
     *
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        ConfigurableApplicationContext applicationContext = SpringApplication.run(OrderAddressSourceApplication.class, args);
        // 初始化省市信息
        initProvince();
        while (true) {
            // 获取订单缓存信息
            Set<String> orderCache = getOrderCache(applicationContext);
            for (String orderInfo : orderCache) {
                String[] orderInfoArr = orderInfo.split(",");
                OrderAddress orderAddress = new OrderAddress();
                // 设置订单信息
                orderAddress.setOrderId(Long.valueOf(orderInfoArr[0]));
                orderAddress.setUserId(Long.valueOf(orderInfoArr[1]));
                if (orderInfoArr.length > 2) {
                    orderAddress.setExecTime(Long.valueOf(orderInfoArr[2]));
                } else {
                    System.out.println("info ==> " + orderInfo);
                }
                // 随机获取省市信息
                Random random = new Random();
                int size = provinceMap.size();
                String[] keys = provinceMap.keySet().toArray(new String[]{});
                String province = keys[random.nextInt(size)];
                List<String> cityList = provinceMap.get(province);
                String city = cityList.get(random.nextInt(cityList.size()));
                orderAddress.setProvince(province);
                orderAddress.setCity(city);
                orderAddress.setAddress(orderAddress.getProvince() + orderAddress.getCity());
                // 保存订单地址信息
                OrderAddressRepository orderAddressRepository = (OrderAddressRepository) applicationContext.getBean("orderAddressRepository");
                orderAddressRepository.save(orderAddress);
                // 写入间隔时间
                Thread.sleep(1000l);
            }
            // 清除缓存信息
            clearCache(applicationContext);
            // 循环间隔时间
            Thread.sleep(1000l);
        }
        
    }
    
    /**
     * 清除缓存
     *
     * @param applicationContext
     * @return
     */
    private static void clearCache(ConfigurableApplicationContext applicationContext) {
        CacheManager cacheManager = (CacheManager) applicationContext.getBean("cacheManager");
        Cache cache = cacheManager.getCache("orderCache");
        cache.clear();
    }
    
    /**
     * 获取订单缓存信息
     *
     * @param orderResult
     * @param applicationContext
     */
    private static Set<String> getOrderCache(ConfigurableApplicationContext applicationContext) {
        
        CacheManager cacheManager = (CacheManager) applicationContext.getBean("cacheManager");
        Cache cache = cacheManager.getCache("orderCache");
        Cache.ValueWrapper orderWrapper = cache.get("orderIds");
        if (null != orderWrapper) {
            Set<String> orderIds = (Set<String>) orderWrapper.get();
            return orderIds;
        }
        return new HashSet<>();
    }
    
    /**
     * 初始化省市信息
     */
    public static void initProvince() {
        List<String> guangdong = new ArrayList<String>();
        guangdong.add("广州市");
        guangdong.add("深圳市");
        guangdong.add("珠海市");
        guangdong.add("佛山市");
        guangdong.add("东莞市");
        guangdong.add("中山市");
        provinceMap.put("广东省", guangdong);
        
        List<String> guangxi = new ArrayList<String>();
        guangxi.add("南宁市");
        guangxi.add("柳州市");
        guangxi.add("桂林市");
        guangxi.add("北海市");
        guangxi.add("贺州市");
        provinceMap.put("广西省", guangxi);
        
        List<String> hubei = new ArrayList<String>();
        hubei.add("武汉市");
        hubei.add("黄石市");
        hubei.add("宜昌市");
        hubei.add("荆州市");
        hubei.add("黄冈市");
        provinceMap.put("湖北省", hubei);
        
        List<String> hunan = new ArrayList<String>();
        hunan.add("长沙市");
        hunan.add("湘潭市");
        hunan.add("衡阳市");
        hunan.add("怀化市");
        hunan.add("娄底市");
        provinceMap.put("湖南省", hunan);
        
        List<String> shanghai = new ArrayList<String>();
        shanghai.add("黄浦区");
        shanghai.add("徐汇区");
        shanghai.add("普陀区");
        shanghai.add("虹口区");
        shanghai.add("浦东新区");
        provinceMap.put("上海市", shanghai);
        
    }
    
}
